package com.heliang.protocol.service;

import java.io.IOException;
import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.heliang.common.FileLog;
import com.heliang.common.StringUtils;
import com.heliang.dataaccess.bean.InfoBean;
import com.heliang.protocol.baseBean.ReqBodyBaseBean;
import com.heliang.protocol.baseBean.RspBodyBaseBean;
import com.heliang.protocol.requestBean.ReqBodyInfoBean;
import com.heliang.protocol.responseBean.RspBodyInfoBean;
import com.heliang.util.Constants;

public class InfoProtocolImpl extends ProtocolBase {

    /**
     * @Description: 网络请求处理
     * @param: @param reqBodyBaseBean
     * @param: @param url
     * @param: @return
     * @param: @throws JSONException
     * @param: @throws IOException
     * @return: RspBodyBaseBean
     * @throws
     * @author heliang
     * @Date 2013-10-21 下午3:16:24
     */
    public static RspBodyBaseBean dataProcess(ReqBodyBaseBean reqBodyBaseBean, String url)
        throws JSONException, IOException {
        String rspData = "";
        ReqBodyInfoBean reqBodyInfoBean = (ReqBodyInfoBean) reqBodyBaseBean;

        /* 构建请求参数 */
        JSONObject reqJsonObject = new JSONObject();
        reqJsonObject.putOpt("keyvalue", reqBodyInfoBean.getKeyValue());
        reqJsonObject.putOpt("ver", reqBodyInfoBean.getVerInfo());
        reqJsonObject.putOpt("site_id", reqBodyInfoBean.getSiteId());
        reqJsonObject.putOpt("cats_id", reqBodyInfoBean.getCatId());
        reqJsonObject.putOpt("updatetime", reqBodyInfoBean.getUpdateTime());
        String dataStr = reqJsonObject.toString();

        /* 发起请求 */
        try {
            url += URLEncoder.encode(StringUtils.encodeBase64(dataStr.getBytes()), "utf-8");
            rspData = getDataByReqServer(url);
            Log.d("Protocol", "request_url:" + url);
        } catch (Exception e) {
            FileLog.log(Constants.TAG + e.getMessage() + "--InfoProtocolImpl.dataProcess --" + "params:"
                + url);
        }

        /* 处理响应结果 */
        RspBodyInfoBean rspBodyInfoBean = new RspBodyInfoBean();
        JSONObject rspJsonObject = new JSONObject(rspData);
        rspBodyInfoBean.setVer(rspJsonObject.optInt("ver"));
        
        
        JSONArray infoArray = rspJsonObject.getJSONArray("infos");
        String pathStr = Constants.SDCARD_PATH + Constants.PATH_INFO;
        String currentTimeStr = System.currentTimeMillis() + "";
        String picName = "";
        // 解析推荐资讯信息
        if (infoArray != null && infoArray.length() > 0) {
            for (int i = 0; i < infoArray.length(); i++) {
                InfoBean infoBean = new InfoBean();
                infoBean.setInfoId(infoArray.getJSONObject(i).getInt("id"));
                infoBean.setType(0);
                if (reqBodyInfoBean.getCatId() == 0) {
                    infoBean.setCatId(0);
                } else {
                    infoBean.setCatId(infoArray.getJSONObject(i).getInt("catid"));
                }
                infoBean.setTitle(infoArray.getJSONObject(i).getString("title"));
                infoBean.setContent(infoArray.getJSONObject(i).getString("desc"));
                infoBean.setCreated(infoArray.getJSONObject(i).getInt("created"));
                infoBean.setUpdateTime(infoArray.getJSONObject(i).getInt("updatetime"));
                infoBean.setCompanyName(infoArray.getJSONObject(i).getString("companyname"));
                infoBean.setCommentCount(infoArray.getJSONObject(i).getInt("comment"));
                if (infoArray.getJSONObject(i).getInt("recommend") == 1) {
                    infoBean.setFirstNews(true);
                } else {
                    infoBean.setFirstNews(false);
                }
                if (infoArray.getJSONObject(i).getInt("push_time") > 0) {
                    infoBean.setRecommend(true);
                } else {
                    infoBean.setRecommend(false);
                }
                JSONArray picsArray = infoArray.getJSONObject(i).optJSONArray("pics");
                if (picsArray != null && picsArray.length() > 0) {
                    picName = "info_" + infoBean.getId() + "_" + currentTimeStr + "_0_1.png";
                    infoBean.setPic1Url(picsArray.getJSONObject(0).optString("pic1"));
                    infoBean.setPic1Path(pathStr + picName);

                    picName = "info_" + infoBean.getId() + "_" + currentTimeStr + "_0_2.png";
                    infoBean.setPic2Url(picsArray.getJSONObject(0).optString("pic2"));
                    infoBean.setPic2Path(pathStr + picName);

                }
                rspBodyInfoBean.getInfoList().add(infoBean);
            }
        }
        JSONArray delsArray = rspJsonObject.optJSONArray("dels");
        if (delsArray != null && delsArray.length() > 0) {
            Integer[] ids = new Integer[delsArray.length()];
            for (int i = 0; i < delsArray.length(); i++) {
                ids[i] = delsArray.getJSONObject(i).optInt("id");
            }
            rspBodyInfoBean.setDelIds(ids);
            ids = null;
        }
        delsArray = null;

        return rspBodyInfoBean;
    }

}
